<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header">
        <el-button
          class="filter-item marginRB10"
          icon="el-icon-plus"
          type="primary"
          @click="handleCreate"
        >添加
        </el-button>
        <el-button class="marginRB10" type="danger" @click="allDel()"
        >批量删除
        </el-button>

        <el-select
          v-model="listQuery.on_sale"
          class="filter-item marginRB10"
          filterable
          placeholder="出售状态"
          style="width: 200px"
        >
          <el-option
            v-for="item in selectList1"
            :key="item.key"
            :label="item.lab"
            :value="item.key"
          ></el-option>
        </el-select>
        <el-select
          v-model="listQuery.category_id"
          class="filter-item marginRB10"
          filterable
          placeholder="请选择类别"
          style="width: 200px"
        >
          <el-option
            v-for="item in searchOptions.category_list"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-date-picker
          v-model="listQuery.add_time"
          class="marginRB10"
          placeholder="添加时间"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
        <el-input
          v-model="listQuery.title"
          class="marginRB10"
          clearable
          placeholder="名称"
          style="width: 200px"
        ></el-input>
        <el-select
          v-model="listQuery.free"
          class="filter-item marginRB10"
          filterable
          placeholder="是否免费"
          style="width: 200px"
        >
          <el-option
            v-for="item in selectList2"
            :key="item.key"
            :label="item.lab"
            :value="item.key"
          ></el-option>
        </el-select>
        <el-select
          v-model="listQuery.forever"
          class="filter-item marginRB10"
          filterable
          placeholder="是否永久"
          style="width: 200px"
        >
          <el-option
            v-for="item in selectList3"
            :key="item.key"
            :label="item.lab"
            :value="item.key"
          ></el-option>
        </el-select>
        <el-input
          v-model="listQuery.day"
          class="marginRB10"
          clearable
          placeholder="天数"
          style="width: 200px"
        ></el-input>
        <el-input
          v-model="listQuery.price"
          class="marginRB10"
          clearable
          placeholder="价格"
          style="width: 200px"
        ></el-input>
        <el-button class="marginRB10" type="primary" @click="searchForm"
        >搜索
        </el-button>
        <el-button class="marginRB10" type="primary" @click="resetList"
        >重置
        </el-button>
      </div>
      <div class="item">
        <el-table
          ref="dataList"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="margin-bottom: 20px"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="名称" prop="title"></el-table-column>
          <el-table-column label="类别" prop="category_name"></el-table-column>
          <el-table-column label="价格" min-width="120" prop="renew">
            <template slot-scope="scope">
              <div style="display: flex; flex-direction: column">
                <span
                  v-for="(item, index) in scope.row.mall_products_price"
                  :key="'价格' + index"
                >{{ item.day }}天：{{ item.price }}金币</span
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="图片" prop="picture">
            <template slot-scope="scope">
              <el-popover max-width="220" placement="right" trigger="hover">
                <img
                  :src="scope.row.picture"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.picture"
                  alt=""
                  style="width: 50px"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="效果" prop="special" width="200">
            <template slot-scope="scope">
              <el-popover
                v-if="/(webp)|(jpg)|(png)|(jpeg)$/i.test(scope.row.special)"
                placement="left"
                trigger="hover"
                width="220"
              >
                <img
                  :src="scope.row.special"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.special"
                  style="height: 25px"
                />
              </el-popover>
              <video
                v-else-if="/\.mp4$/i.test(scope.row.special)"
                :src="scope.row.special"
                class="video"
                controls
              ></video>
              <p v-else>{{ scope.row.special }}</p>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="on_sale">
            <template slot-scope="scope">
              {{ scope.row.on_sale === 1 ? '出售中' : '' }}
              {{ scope.row.on_sale === 2 ? '非卖品' : '' }}
            </template>
          </el-table-column>
          <el-table-column label="添加时间" prop="add_time"></el-table-column>

          <el-table-column label="操作" min-width="240">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                type="primary"
                @click="handleUpdate(scope.row)"
              >编辑
              </el-button>
              <cus-del-btn
                style="margin-right: 10px"
                @ok="handleDelete(scope.row)"
              ></cus-del-btn>
              <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                type="primary"
                @click="handleUpdate2(scope.row)"
              >赠送
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          v-show="total > 0"
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :model="form"
        :rules="rules"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="选择类别" prop="category_id">
          <el-select
            v-model="form.category_id"
            class="filter-item marginRB10"
            filterable
            placeholder="请选择类别"
            style="width: 200px"
          >
            <el-option
              v-for="item in searchOptions.category_list"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="展示位置" prop="show_type" style="width:500px">
          <el-radio v-model="form.show_type" label="1">公屏</el-radio>
          <el-radio v-model="form.show_type" label="2">全屏</el-radio>
          <el-radio v-model="form.show_type" label="3">新版坐骑</el-radio>
          <div style="line-height: 0; color: #dd514c">该设置仅对坐骑有效</div>
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagList"

            :limit="1"
            :on-remove="handleSpecialFlagRemove"
            :on-success="handleSpecialFlagSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item v-if="form.category_id === 4" label="字体颜色">
          <el-input v-model="form.special"></el-input>
        </el-form-item>
        <el-form-item v-else-if="form.category_id === 2" label="回声样式">
          <el-select v-model="form.special">
            <el-option
              v-for="item in bubble_options"
              :key="'回声' + item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="效果图">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList"

            :limit="1"
            :on-remove="handleSpecialFlagBigRemove"
            :on-success="handleSpecialFlagBigSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="天数 & 价格" prop="moods">
          <div
            v-for="(i, index) in form.day_price"
            :key="'人气' + index"
            style="margin-bottom: 10px"
          >
            <el-input v-model="i.day" placeholder="天数" style="width: 80px"/>
            <el-input
              v-model="i.price"
              placeholder="价格"
              style="width: 80px"
            />
          </div>
          <el-button plain type="primary" @click="newDandP()">+</el-button>
        </el-form-item>
        <el-form-item label="状态" prop="on_sale">
          <el-radio v-model="form.on_sale" label="1">出售</el-radio>
          <el-radio v-model="form.on_sale" label="2">非卖品</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      v-dialogDrag
      :visible.sync="dialogFormVisible2"
      title="赠送"
      @close="handleDialogClose2"
    >
      <el-form
        ref="dataForm2"
        :model="form2"
        :rules="rules2"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="商品名称">
          {{ form2.title }}
        </el-form-item>
        <el-form-item label="图片">
          <img
            :src="form2.picture"
            alt=""
            style="max-width: 200px; max-height: 200px"
          />
        </el-form-item>
        <el-form-item label="效果图">
          <img
            :src="form2.special"
            alt=""
            style="max-width: 200px; max-height: 200px"
          />
        </el-form-item>
        <el-form-item label="用户ID" prop="user_code">
          <el-input v-model="form2.user_code" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="使用天数" prop="use_days">
          <el-input v-model="form2.use_days" placeholder="请输入使用天数"/>
          <!-- <el-date-picker
            class="marginRB10"
            v-model="form2.expired_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="过期时间"
            style="width: 280px; height: 36px"
          ></el-date-picker> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible2 = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm2()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteData, listData, productGive, saveData, searchList, updateData } from '@/api/mall/mallProducts'

export default {
  data() {
    return {

      confirmBtn: false,

      specialFlagBigList: [],
      specialFlagList: [],

      bubble_options: [
        {
          value: 1,
          label: '样式一'
        },
        {
          value: 2,
          label: '样式二'
        },
        {
          value: 3,
          label: '样式三'
        },
        {
          value: 4,
          label: '样式四'
        },
        {
          value: 5,
          label: '样式五'
        },
        {
          value: 6,
          label: '样式六'
        },
        {
          value: 7,
          label: '样式七'
        },
        {
          value: 8,
          label: '样式八'
        }
      ],

      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
        on_sale: undefined,
        category_id: undefined,
        add_time: undefined,
        title: undefined,
        free: undefined,
        forever: undefined,
        day: undefined,
        price: undefined
      },
      menuList: [],
      searchOptions: [],
      selectList1: [
        { key: undefined, lab: '全部出售状态' },
        { key: '1', lab: '出售中 ' },
        { key: '2', lab: '非卖品' }
      ],
      selectList2: [
        { key: undefined, lab: '全部免费状态' },
        { key: '1', lab: '免费 ' },
        { key: '2', lab: '不免费' }
      ],
      selectList3: [
        { key: undefined, lab: '全部永久状态' },
        { key: '1', lab: '永久 ' },
        { key: '2', lab: '不永久' }
      ],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      form: {
        id: undefined,
        category_id: undefined,
        title: undefined,
        show_type: undefined,
        picture: undefined,
        special: undefined,
        day_price: [{ day: '', price: '' }],
        on_sale: undefined
      },
      rules: {
        category_id: [
          { required: true, message: '请输入类别', trigger: 'blur' }
        ],
        title: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        show_type: [
          { required: true, message: '请输入展示位置', trigger: 'blur' }
        ],
        picture: [{ required: true, message: '请输入图片', trigger: 'blur' }],
        on_sale: [{ required: true, message: '请输入状态', trigger: 'blur' }]
      },
      form2: {
        id: undefined,
        title: undefined,
        picture: undefined,
        special: undefined,
        user_code: undefined,
        use_days: undefined
      },
      rules2: {
        user_code: [
          { required: true, message: '请输入用户ID', trigger: 'blur' }
        ],
        use_days: [
          { required: true, message: '请输入使用天数', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getSearchList()
  },
  methods: {
    searchForm() {
      this.listQuery.page = 1
      this.getList()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      // file);
      this.form.picture = file
      // return isJPG && isLt2M;
    },
    handleSpecialFlagBigSuccess(res) {
      this.form.special = res.data.url
      this.specialFlagBigList = [{ url: res.data.url }]
    },
    handleSpecialFlagBigRemove(res) {
      this.specialFlagBigList = []
    },
    handleSpecialFlagSuccess(res) {
      this.form.picture = res.data.url
      this.specialFlagList = [{ url: res.data.url }]
    },
    handleSpecialFlagRemove(res) {
      this.specialFlagList = []
    },
    getSearchList() {
      searchList().then(response => {
        this.searchOptions = response.data
        this.searchOptions.category_list = [
          { id: undefined, title: '全部状态' },
          ...response.data.category_list
        ]
      })
    },
    getList() {
      this.listLoading = true
      for (let key in this.listQuery) {
        this.listQuery[key] =
          this.listQuery[key] === '' || this.listQuery[key] === null
            ? undefined
            : this.listQuery[key]
      }
      listData(this.listQuery).then(response => {
        this.menuList = response.data.list
        this.total = response.data.meta.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    handleCreate(row) {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.form = []
      this.form.id = row.id
      this.form.category_id = row.category_id
      this.form.title = row.title
      this.form.show_type = row.show_type + ''
      this.form.picture = row.picture
      this.specialFlagList = [{ url: row.picture }]
      if (row.special) {
        this.form.special = row.special
        this.specialFlagBigList = [{ url: row.special }]
      }
      this.form.day_price = row.mall_products_price
      this.form.on_sale = row.on_sale + ''

      this.dialogStatus = 'update'
      this.dialogFormVisible = true

      this.form = Object.assign({}, this.form)
      // this.form);
    },
    handleUpdate2(row) {
      this.form2 = []
      this.form2.id = row.id
      this.form2.title = row.title
      this.form2.picture = row.picture
      this.form2.special = row.special

      this.dialogStatus = 'update'
      this.dialogFormVisible2 = true

      this.form = Object.assign({}, this.form)
    },
    handleDelete(row) {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let data = { ids: row.id }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    allDel() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let delNum = []
      this.$refs.dataList.selection.forEach(k => {
        delNum.push(k.id)
      })
      if (delNum.length === 0) {
        return
      }
      let data = { ids: delNum.toString() }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    submitForm() {
      if (this.confirmBtn) return
      this.$refs['dataForm'].validate(valid => {
        if (valid && this.dialogStatus === 'create') {
          this.confirmBtn = true
          let obj = {}
          for (let key in this.form) {
            if (
              this.form[key] !== undefined &&
              this.form[key] !== null &&
              this.form[key] !== ''
            ) {
              obj[key] = this.form[key]
            }
          }
          // obj);
          saveData(obj).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        } else if (valid && this.dialogStatus === 'update') {
          this.confirmBtn = true
          let obj = {}
          for (let key in this.form) {
            if (
              this.form[key] !== undefined &&
              this.form[key] !== null &&
              this.form[key] !== ''
            ) {
              obj[key] = this.form[key]
            }
          }
          updateData(obj).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        }
      })
    },
    submitForm2() {
      if (this.confirmBtn) return
      this.$refs['dataForm2'].validate(valid => {
        this.confirmBtn = true
        let formData = new FormData()
        for (let item in this.form2) {
          this.form2[item] === undefined
            ? ''
            : formData.append(item, this.form2[item])
        }
        productGive(formData).then(response => {
          if (response.code === 200) {
            this.getList()
            this.$notify({
              type: 'success',
              message: '操作成功',
              position: 'bottom-right',
              showClose: false,
              duration: 3000
            })
            this.dialogFormVisible2 = false
          } else {
            this.confirmBtn = false
          }
        })
      })
    },
    resetList() {
      this.listQuery = {
        page: 1,
        perPage: 10,
        on_sale: undefined,
        category_id: undefined,
        add_time: undefined,
        title: undefined,
        free: undefined,
        forever: undefined,
        day: undefined,
        price: undefined
      }
    },
    resetForm() {
      this.form = {
        id: undefined,
        category_id: undefined,
        title: undefined,
        show_type: undefined,
        picture: undefined,
        special: undefined,
        day_price: [{ day: '', price: '' }],
        on_sale: undefined
      }
      this.specialFlagList = []
      this.specialFlagBigList = []
    },
    resetForm2() {
      this.form2 = {
        id: undefined,
        title: undefined,
        picture: undefined,
        special: undefined,
        user_code: undefined,
        expired_time: undefined
      }
    },
    newDandP() {
      this.form.day_price = this.form.day_price
        ? [...this.form.day_price, { day: '', price: '' }]
        : [{ day: '', price: '' }]
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    },
    handleDialogClose2() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>
<style scoped>
.video {
  width  : 200px;
  height : 120px;
}

/deep/ .el-upload-list__item {
  transition : none !important;
}

/deep/ .el-upload-list--picture {
  max-height : 103px !important;
  overflow   : hidden;
}
</style>
